﻿define(['jquery', 'kendo', 'moment', 'ajax-pre'], function ($, kendo, moment) {

    return function (userId, gridEl) {
        var dataSource = new kendo.data.DataSource({
            schema: {
                data: function (response) {
                    return response;
                },
                model: {
                    id: "Id",
                    fields: {
                        Id: { nullable: false },
                        QualName: { validation: { required: true } },
                        QualType: { validation: { required: true } },
                        QualLevel: { validation: { required: true } },
                        ValidStartDate: {type: "date", validation: { required: true }, defaultValue: new Date() },
                        ValidEndDate: { type: "date", validation: { required: true }, defaultValue: new Date() },
                        Remark: { validation: { required: false }, defaultValue: null },
                        UserId: { validation: { required: true }, defaultValue: userId },
                    }
                }
            },
            serverPaging: false,
            batch: true,
            transport: {
                read: {
                    url: "/Qualification/GetByUserId",
                    dataType: "json",
                    method: "post"
                },
                update: {
                    url: "/Qualification/Save",
                    dataType: "json",
                    method: "post",
                    contentType: "application/json"
                },
                destroy: {
                    url: "/Qualification/Remove",
                    dataType: "json",
                    method: "post",
                    contentType: "application/json"
                },
                create: {
                    url: "/Qualification/Save",
                    dataType: "json",
                    method: "post",
                    contentType: "application/json"
                },
                parameterMap: function (options, operation) {
                    if (operation !== "read" && options.models) {
                        return JSON.stringify({ entitys: options.models });
                    }
                    if (operation == "read") {
                        return {
                            userId:userId,
                            page: {
                                page: options.page,
                                pages: options.pageSize
                            }
                        };
                    }
                }
            }
        });

        $(gridEl).kendoGrid({
            dataSource: dataSource,
            filterable: true,
            toolbar: [{ name: "create", text: '新建' }, { name: "save", text: '保存' }],
            pageable: {
                refresh: true,
                pageSizes: true,
                buttonCount: 5
            },
            columns: [
                { field: "QualName", title: "资质名", width: '180px' },
                { field: "QualType", title: "资质类型(语种)" },
                { field: "QualLevel", title: "资质等级" },
                {
                    field: "ValidStartDate", title: "有效期开始", width: '150px',
                    template: function (value) {
                        return new moment(value.ValidStartDate).format('YYYY-MM-DD');
                    },
                    editor: function (container, options) {
                        $('<input data-bind="value:' + options.field + '" />').appendTo(container)
                        .kendoDatePicker({
                            format: 'yyyy-MM-dd',
                            interval: 1
                        });
                    }
                },
                 {
                     field: "ValidEndDate", title: "有效期结束", width: '150px',
                     template: function (value) {
                         return new moment(value.ValidEndDate).format('YYYY-MM-DD');
                     },
                     editor: function (container, options) {
                         $('<input data-bind="value:' + options.field + '" />').appendTo(container)
                         .kendoDatePicker({
                             format: 'yyyy-MM-dd',
                             interval: 1
                         });
                     }
                 },
                 { field: "Remark", title: "备注" },
                {
                    width: '180px',
                    command: [
                      { name: "destroy", 'text': '删除' },
                      { name: "edit", text: '编辑' }
                    ]
                }
            ],
            editable: { //disables the deletion functionality
                update: true,
                destroy: true,
                create: true,
                mode: 'inline',// inline popup
            },
            //editable: "popup",
            pageable: false,

        });
    }
});